﻿namespace WMS6.Web.Analytics
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.CustomsServiceRef;
    using WMS6.Web.UserControls;

    public class DeclarationsBlanaceRptList : BasePage
    {
        protected Button btnExport;
        protected Button btnList;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F15531;
        protected GridView gvInOutBlanaceRptList;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnShowLot;
        protected HtmlHead Head1;
        protected Label lblFinanceInOutStockDetailRptList;
        protected Label lblMsg;
        protected Label lblTotalBeginningQty;
        protected Label lblTotalBeginningQtyText;
        protected Label lblTotalCurrentIssueInboundQty;
        protected Label lblTotalCurrentIssueInboundQtyText;
        protected Label lblTotalCurrentIssueOutBoundQty;
        protected Label lblTotalCurrentIssueOutBoundQtyText;
        protected Label lblTotalQty;
        protected Label lblTotalQtyText;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        private decimal TotalBeginningQty;
        private decimal TotalCurrentIssueInboundQty;
        private decimal TotalCurrentIssueOutBoundQty;
        private decimal TotalQty;
        protected UpdatePanel up1;

        public void BindData2InOutBlanaceRpt(DeclarationsBlanaceRptQueryEntity declarationsBlanaceRptQueryEntity)
        {
            this.PagerFooter.CurrentPage = int.Parse(declarationsBlanaceRptQueryEntity.CurrentPage);
            DataSet ds = CustomsServiceClientProxy.GetInstance().GetDeclarationsBlanaceRptDataSet(declarationsBlanaceRptQueryEntity);
            base.BindData2GridView(this.gvInOutBlanaceRptList, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            DeclarationsBlanaceRptQueryEntity declarationsBlanaceRptQueryEntity = this.GetDeclarationsBlanaceRptQueryEntity();
            declarationsBlanaceRptQueryEntity.IsGetAll = true;
            this.hdnActionID.Value = "EXPORT";
            string currentPageIndex = declarationsBlanaceRptQueryEntity.CurrentPage;
            declarationsBlanaceRptQueryEntity.CurrentPage = "0";
            try
            {
                using (DataSet ds = CustomsServiceClientProxy.GetInstance().GetDeclarationsBlanaceRptDataSet(declarationsBlanaceRptQueryEntity))
                {
                    base.Export2Excel(" DeclarationsBlanaceRptList.xls", "15531", this.gvInOutBlanaceRptList, ds);
                }
            }
            finally
            {
                declarationsBlanaceRptQueryEntity.IsGetAll = false;
                declarationsBlanaceRptQueryEntity.CurrentPage = currentPageIndex;
                this.SetDeclarationsBlanaceRptQueryEntity(declarationsBlanaceRptQueryEntity);
            }
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            DeclarationsBlanaceRptQueryEntity declarationsBlanaceRptQueryEntity = this.GetDeclarationsBlanaceRptQueryEntity();
            declarationsBlanaceRptQueryEntity.CurrentPage = "0";
            this.BindData2InOutBlanaceRpt(declarationsBlanaceRptQueryEntity);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            DeclarationsBlanaceRptQueryEntity declarationsBlanaceRptQueryEntity = this.GetDeclarationsBlanaceRptQueryEntity();
            declarationsBlanaceRptQueryEntity.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2InOutBlanaceRpt(declarationsBlanaceRptQueryEntity);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            DeclarationsBlanaceRptQueryEntity declarationsBlanaceRptQueryEntity = this.GetDeclarationsBlanaceRptQueryEntity();
            declarationsBlanaceRptQueryEntity.OwnerID = "";
            declarationsBlanaceRptQueryEntity.SkuID = "";
            declarationsBlanaceRptQueryEntity.ItemNoID = "";
            declarationsBlanaceRptQueryEntity.StartTime = "";
            declarationsBlanaceRptQueryEntity.EndTime = "";
            this.BindData2InOutBlanaceRpt(declarationsBlanaceRptQueryEntity);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            DeclarationsBlanaceRptQueryEntity declarationsBlanaceRptQueryEntity = this.GetDeclarationsBlanaceRptQueryEntity();
            if (int.Parse(declarationsBlanaceRptQueryEntity.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                declarationsBlanaceRptQueryEntity.CurrentPage = (int.Parse(declarationsBlanaceRptQueryEntity.CurrentPage) + 1).ToString();
                this.BindData2InOutBlanaceRpt(declarationsBlanaceRptQueryEntity);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            DeclarationsBlanaceRptQueryEntity declarationsBlanaceRptQueryEntity = this.GetDeclarationsBlanaceRptQueryEntity();
            if (int.Parse(declarationsBlanaceRptQueryEntity.CurrentPage) > 0)
            {
                declarationsBlanaceRptQueryEntity.CurrentPage = (int.Parse(declarationsBlanaceRptQueryEntity.CurrentPage) - 1).ToString();
                this.BindData2InOutBlanaceRpt(declarationsBlanaceRptQueryEntity);
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            DeclarationsBlanaceRptQueryEntity declarationsBlanaceRptQueryEntity = this.GetDeclarationsBlanaceRptQueryEntity();
            this.BindData2InOutBlanaceRpt(declarationsBlanaceRptQueryEntity);
        }

        private DeclarationsBlanaceRptQueryEntity GetDeclarationsBlanaceRptQueryEntity()
        {
            return (base.PageQueryEntitySession as DeclarationsBlanaceRptQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                DeclarationsBlanaceRptQueryEntity declarationsBlanaceRptQueryEntity = this.GetDeclarationsBlanaceRptQueryEntity();
                declarationsBlanaceRptQueryEntity.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2InOutBlanaceRpt(declarationsBlanaceRptQueryEntity);
            }
        }

        protected void gvInOutBlanaceRptList_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F15531", this.gvInOutBlanaceRptList);
        }

        protected void gvInOutBlanaceRptList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                e.Row.Attributes.Add("ondblclick", "JavaScript:document.getElementById('" + ((CheckBox) e.Row.FindControl("chbSelect")).ClientID + "').click();");
                if (this.hdnActionID.Value != "EXPORT")
                {
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblDESCR"));
                }
                if (this.hdnActionID.Value != "EXPORT")
                {
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblDESCR2"));
                }
                if (((Label) e.Row.FindControl("lblBeginningQty")) != null)
                {
                    this.TotalBeginningQty += decimal.Parse((((Label) e.Row.FindControl("lblBeginningQty")).Text != "") ? ((Label) e.Row.FindControl("lblBeginningQty")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblCurrentIssueInboundQty")) != null)
                {
                    this.TotalCurrentIssueInboundQty += decimal.Parse((((Label) e.Row.FindControl("lblCurrentIssueInboundQty")).Text != "") ? ((Label) e.Row.FindControl("lblCurrentIssueInboundQty")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblCurrentIssueOutBoundQty")) != null)
                {
                    this.TotalCurrentIssueOutBoundQty += decimal.Parse((((Label) e.Row.FindControl("lblCurrentIssueOutBoundQty")).Text != "") ? ((Label) e.Row.FindControl("lblCurrentIssueOutBoundQty")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblTotalQty")) != null)
                {
                    this.TotalQty += decimal.Parse((((Label) e.Row.FindControl("lblTotalQty")).Text != "") ? ((Label) e.Row.FindControl("lblTotalQty")).Text : "0");
                }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                this.lblTotalBeginningQtyText.Text = base.GetPreferenceDigit(this.TotalBeginningQty);
                this.lblTotalCurrentIssueInboundQtyText.Text = base.GetPreferenceDigit(this.TotalCurrentIssueInboundQty);
                this.lblTotalCurrentIssueOutBoundQtyText.Text = base.GetPreferenceDigit(this.TotalCurrentIssueOutBoundQty);
                this.lblTotalQtyText.Text = base.GetPreferenceDigit(this.TotalQty);
            }
        }

        protected void gvInOutBlanaceRptList_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                DeclarationsBlanaceRptQueryEntity declarationsBlanaceRptQueryEntity = this.GetDeclarationsBlanaceRptQueryEntity();
                declarationsBlanaceRptQueryEntity.SortField = e.SortExpression;
                if ((declarationsBlanaceRptQueryEntity.SortDirection == "ASC") || (declarationsBlanaceRptQueryEntity.SortDirection.Trim() == ""))
                {
                    declarationsBlanaceRptQueryEntity.SortDirection = "DESC";
                }
                else if (declarationsBlanaceRptQueryEntity.SortDirection == "DESC")
                {
                    declarationsBlanaceRptQueryEntity.SortDirection = "ASC";
                }
                this.BindData2InOutBlanaceRpt(declarationsBlanaceRptQueryEntity);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            DeclarationsBlanaceRptQueryEntity declarationsBlanaceRptQueryEntity = new DeclarationsBlanaceRptQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    declarationsBlanaceRptQueryEntity.OwnerID = this.PreviousPage.OwnerID;
                    declarationsBlanaceRptQueryEntity.SkuID = this.PreviousPage.SkuID;
                    declarationsBlanaceRptQueryEntity.ItemNoID = this.PreviousPage.ItemNoID;
                    declarationsBlanaceRptQueryEntity.StartTime = this.PreviousPage.StartTime;
                    declarationsBlanaceRptQueryEntity.EndTime = this.PreviousPage.EndTime;
                }
                else
                {
                    declarationsBlanaceRptQueryEntity.OwnerID = "";
                    declarationsBlanaceRptQueryEntity.SkuID = "";
                    declarationsBlanaceRptQueryEntity.ItemNoID = "";
                    declarationsBlanaceRptQueryEntity.StartTime = "";
                    declarationsBlanaceRptQueryEntity.EndTime = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            declarationsBlanaceRptQueryEntity.PageSize = this.gvInOutBlanaceRptList.PageSize.ToString();
            this.PagerFooter.PageSize = this.gvInOutBlanaceRptList.PageSize;
            declarationsBlanaceRptQueryEntity.IsGetAll = false;
            declarationsBlanaceRptQueryEntity.CurrentPage = "0";
            declarationsBlanaceRptQueryEntity.SortField = "A.OWNER_ID, A.SKU_ID";
            declarationsBlanaceRptQueryEntity.SortDirection = "";
            this.SetDeclarationsBlanaceRptQueryEntity(declarationsBlanaceRptQueryEntity);
            this.BindData2InOutBlanaceRpt(declarationsBlanaceRptQueryEntity);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                base.InitAnalyticsReportGridview(this.gvInOutBlanaceRptList);
                this.LoadData();
            }
        }

        public void SetDeclarationsBlanaceRptQueryEntity(DeclarationsBlanaceRptQueryEntity declarationsBlanaceRptQueryEntity)
        {
            base.PageQueryEntitySession = declarationsBlanaceRptQueryEntity;
        }

        public DeclarationsBlanaceRptQuery PreviousPage
        {
            get
            {
                return (DeclarationsBlanaceRptQuery) base.PreviousPage;
            }
        }
    }
}

